package com.lmbx.shzh.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @Auther: wyl
 * @Date: 2021/5/12
 * @Description:
 */
@Slf4j
public class JsonUtils {

    private static Gson gson;

    static {
        GsonBuilder builder = new GsonBuilder();
        gson = builder.create();
    }

    /**
     * 将Java对象进行JSON序列化
     * <p>
     * 支持自定义日期时间类型格式
     * <p>
     *
     * @param pObject
     * @param pDateFormat 日期时间类型格式字符串
     * @return
     */
    public static final String toJson(Object pObject, String pDateFormat) {
        String jsonString = "";
        if (pDateFormat == null || pDateFormat.length() == 0) {
            pDateFormat = "yyyy-MM-dd HH:mm:ss";
        }
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat(pDateFormat);
        Gson gson = builder.serializeNulls().create();//serializeNulls值为空时,保留该属性
        jsonString = gson.toJson(pObject);
        return jsonString;
    }

    /**
     * 将Java对象进行JSON序列化
     * <p>
     * 缺省的日期时间类型为：yyyy-MM-dd HH:mm:ss
     * <p>
     *
     * @param pObject
     * @return
     */
    public static final String toJson(Object pObject) {
        String jsonString = toJson(pObject, "yyyy-MM-dd HH:mm:ss");
        return jsonString;
    }

    /**
     * 将Json字符串转换为Java对象
     *
     * @param json
     * @param obj
     * @param <T>
     * @return
     */
    public static final <T> Object toObj(String json, Class<T> obj) {
        T res = gson.fromJson(json, obj);
        return res;
    }

    /**
     * 将Json字符串转换为List集合
     *
     * @param jsonStr
     * @param <T>
     * @return
     */
    public static final <T> List<T> toObjList(String jsonStr) {
        List<T> list = gson.fromJson(jsonStr,
                new TypeToken<Collection<HashMap<String, String>>>() {
                }.getType());
        return list;
    }

    /**
     * 将Json字符串转换为List集合
     *
     * @param jsonStr
     * @param <T>
     * @return
     */
    public static final <T> List<T> toObjList(String jsonStr, Class<T> clazz) {
        Type listType = new ParameterizedTypeImpl(List.class, new Class[]{clazz});
        List<T> list = gson.fromJson(jsonStr, listType);
        return list;
    }

    /**
     * 自己生成Type
     */
    private static class ParameterizedTypeImpl implements ParameterizedType {
        private final Class raw;
        private final Type[] args;

        public ParameterizedTypeImpl(Class raw, Type[] args) {
            this.raw = raw;
            this.args = args != null ? args : new Type[0];
        }

        @Override
        public Type[] getActualTypeArguments() {
            return args;
        }

        @Override
        public Type getRawType() {
            return raw;
        }

        @Override
        public Type getOwnerType() {
            return null;
        }
    }

}
